﻿using FS.Common.DotNetCode;
using FS.Common.DotNetConfig;
using System;
using System.Web;

namespace FS.Web
{
    public class Global : HttpApplication
    {
        protected LogHelper Logger = new LogHelper("Global");
        private void Application_Start(object sender, EventArgs e)
        {
            base.Application.Lock();
            base.Application["CurrentUsers"] = 0;
            base.Application.UnLock();           
        }
        private void Application_Error(object sender, EventArgs e)
        {
            Exception objErr = base.Server.GetLastError().GetBaseException();
            string error = objErr.Message ?? "";            
            
            this.Logger.WriteLog(string.Concat(new string[]
				{
					"-----------系统错误-----------\r\n",
					"发生异常页: ",
                    Request.Url.ToString() ,
                    "\r\n",
                    "异常信息: ",
                    error ,
                    "\r\n",
					 "错误源:",
                     objErr.Source,
                     "\r\n",
                     "堆栈信息:" ,
                     objErr.StackTrace,					
					"\r\n"
				}));
            base.Server.ClearError();
            base.Application["error"] = error;
            base.Response.Redirect("~/Frame/ErrorPage.aspx");
            
        }
        private void Session_Start(object sender, EventArgs e)
        {
            base.Application.Lock();
            base.Application["CurrentUsers"] = (int)base.Application["CurrentUsers"] + 1;
            base.Application.UnLock();
        }
        private void Session_End(object sender, EventArgs e)
        {
            base.Application.Lock();
            base.Application["CurrentUsers"] = (int)base.Application["CurrentUsers"] - 1;
            base.Application.UnLock();
        }        
    }
}
